package com.dongdongshop.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.mapper.TbOrderItemMapper;
import com.dongdongshop.mapper.TbOrderMapper;
import com.dongdongshop.mapper.TbPayLogMapper;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.OrderService;
import com.dongdongshop.utils.IdWorker;
import com.dongdongshop.vo.Cart;
import com.dongdongshop.vo.OderOderItemSend;
import com.dongdongshop.vo.OrderOrderItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

@Service
@org.springframework.stereotype.Service
public class OrderServiceImp implements OrderService {
    @Autowired
    private TbOrderItemMapper tbOrderItemMapper;
    @Autowired
    private TbOrderMapper tbOrderMapper;
    @Autowired
    private TbPayLogMapper tbPayLogMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    @Transactional
    public int addOrder(TbOrder order) {
        IdWorker idWorker = new IdWorker(0, 0);
        List<Cart> cartList = (List<Cart>) redisTemplate.boundHashOps("cartList").get(order.getUserId());
        for (Cart cart:cartList) {
            long nextId = idWorker.nextId();
            TbOrder tbOrder = new TbOrder();
            tbOrder.setOrderNum(order.getOrderNum());
            tbOrder.setOrderId(nextId);
            tbOrder.setUserId(order.getUserId());
            tbOrder.setPaymentType("1");
            tbOrder.setStatus("1");
            tbOrder.setCreateTime(new Date());
            tbOrder.setUpdateTime(new Date());
            tbOrder.setReceiverAreaName(order.getReceiverAreaName());
            tbOrder.setReceiverMobile(order.getReceiverMobile());
            tbOrder.setReceiver(order.getReceiver());
            tbOrder.setSourceType("2");
            tbOrder.setSellerId(cart.getSellerId());
            tbOrder.setPostFee("1");
            tbOrder.setPaymentTime(new Date());
            tbOrder.setConsignTime(new Date());
            tbOrder.setEndTime(new Date());
            tbOrder.setCloseTime(new Date());
            tbOrder.setShippingCode("1");
            tbOrder.setShippingName("1");
            tbOrder.setBuyerMessage("1");
            tbOrder.setBuyerNick("1");
            tbOrder.setBuyerRate("1");
            tbOrder.setExpire(new Date());
            tbOrder.setInvoiceType("1");
            double money = 0;
            for (TbOrderItem orderItem:cart.getOrderItemList()) {
                orderItem.setId(idWorker.nextId());
                orderItem.setOrderId(nextId);
                orderItem.setSellerId(cart.getSellerId());
                orderItem.setOrderNum(order.getOrderNum());
                money += orderItem.getTotalFee().doubleValue();
                tbOrderItemMapper.insertSelective(orderItem);
            }
            tbOrder.setPayment(new BigDecimal((money)));
            tbOrderMapper.insertSelective(tbOrder);
        }
        redisTemplate.boundHashOps("cartList").delete(order.getUserId());

        return 1;
    }

    @Override
    public void updateOrderStatus(int i, String out_trade_no, String trade_no) {
        TbOrder tbOrder = new TbOrder();
        tbOrder.setStatus(i + "");
        tbOrder.setOrderNum(out_trade_no);
        tbOrder.setSerialNum(trade_no);
        tbOrderMapper.updateOrderNum(tbOrder.getOrderNum(), tbOrder.getSerialNum(), tbOrder.getStatus());
        tbOrderItemMapper.updateOrderItem(tbOrder.getOrderNum(),tbOrder.getSerialNum());

    }

    @Override
    public OrderOrderItemVO selectUser(String out_trade_no) {
        TbOrderExample te = new TbOrderExample();
        te.createCriteria().andOrderNumEqualTo(out_trade_no);
        List<TbOrder> tbOrders = tbOrderMapper.selectByExample(te);
        OrderOrderItemVO orderOrderItemVO = new OrderOrderItemVO();
        orderOrderItemVO.setUsername(tbOrders.get(0).getUserId());
        TbOrderItemExample ti = new TbOrderItemExample();
        ti.createCriteria().andOrderNumEqualTo(out_trade_no);
        List<TbOrderItem> orderItemList = tbOrderItemMapper.selectByExample(ti);
        StringBuilder str = new StringBuilder();
        for (TbOrderItem tbOrderItem : orderItemList) {
            str.append("," + tbOrderItem.getId());
        }
        String order_list = str.toString();
        orderOrderItemVO.setOrder_list( order_list.substring(1));
        return orderOrderItemVO;
    }

    @Override
    public void addInfoLog(TbPayLog tbPayLog) {
        tbPayLogMapper.insertSelective(tbPayLog);
    }

    @Override
    public  List<OderOderItemSend> sendShow(TbUser user, String status) {
        TbOrderExample example = new TbOrderExample();
        TbOrderExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(user.getUsername());
        criteria.andStatusEqualTo(status);
        List<TbOrder> tbOrders = tbOrderMapper.selectByExample(example);

        List<OderOderItemSend> cartList = new ArrayList<>();
        for (TbOrder tbOrder : tbOrders) {
            OderOderItemSend cartTwo = new OderOderItemSend();
            cartTwo.setOrder(tbOrder);

            TbOrderItemExample tbOrderItemExample = new TbOrderItemExample();
            TbOrderItemExample.Criteria criteria1 = tbOrderItemExample.createCriteria();
            criteria1.andOrderIdEqualTo(tbOrder.getOrderId());
            List<TbOrderItem> tbOrderItemList = tbOrderItemMapper.selectByExample(tbOrderItemExample);
            cartTwo.setOrderItemList(tbOrderItemList);
            cartList.add(cartTwo);
        }
        return cartList;
    }

    @Override
    public TbOrder selectByOrder(String out_trade_no) {
        TbOrderExample tbOrderExample = new TbOrderExample();
        tbOrderExample.createCriteria().andOrderNumEqualTo(out_trade_no);
        List<TbOrder> orderList = tbOrderMapper.selectByExample(tbOrderExample);
        return orderList.get(0);
    }

    @Override
    public void updateByOrder(String orderNum, String serialNum) {
        String status = "3";
        tbOrderMapper.updateOrderNum(orderNum,serialNum,status);
    }

    //退款
    @Override
    public void updateOrderRefundStatus(String out_trade_no, String status) {
        tbOrderMapper.updateOrderRefundStatus(out_trade_no,status);
    }

}
